package com.atguigu.campusintelligentassistantsystem.controller;

import com.atguigu.campusintelligentassistantsystem.entity.ActivityRegistration;
import com.atguigu.campusintelligentassistantsystem.entity.CampusActivity;
import com.atguigu.campusintelligentassistantsystem.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/activities")
@CrossOrigin(origins = "*")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 发布校园活动
     * @param activity 活动信息
     * @return 保存后的活动
     */
    @PostMapping
    public ResponseEntity<CampusActivity> publishActivity(@RequestBody CampusActivity activity) {
        CampusActivity savedActivity = activityService.publishActivity(activity);
        return ResponseEntity.ok(savedActivity);
    }

    /**
     * 获取所有活动
     * @return 活动列表
     */
    @GetMapping
    public ResponseEntity<List<CampusActivity>> getAllActivities() {
        List<CampusActivity> activities = activityService.getAllActivities();
        return ResponseEntity.ok(activities);
    }

    /**
     * 根据状态获取活动
     * @param status 状态
     * @return 活动列表
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<CampusActivity>> getActivitiesByStatus(@PathVariable String status) {
        List<CampusActivity> activities = activityService.getActivitiesByStatus(status);
        return ResponseEntity.ok(activities);
    }

    /**
     * 根据ID获取活动
     * @param id 活动ID
     * @return 活动信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<CampusActivity> getActivityById(@PathVariable Long id) {
        CampusActivity activity = activityService.getActivityById(id);
        if (activity != null) {
            return ResponseEntity.ok(activity);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 更新活动信息
     * @param id 活动ID
     * @param activity 活动信息
     * @return 更新后的活动
     */
    @PutMapping("/{id}")
    public ResponseEntity<CampusActivity> updateActivity(@PathVariable Long id, @RequestBody CampusActivity activity) {
        activity.setId(id);
        CampusActivity updatedActivity = activityService.updateActivity(activity);
        return ResponseEntity.ok(updatedActivity);
    }

    /**
     * 删除活动
     * @param id 活动ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteActivity(@PathVariable Long id) {
        activityService.deleteActivity(id);
        return ResponseEntity.ok("活动删除成功");
    }

    /**
     * 学生报名活动
     * @param activityId 活动ID
     * @param studentId 学生ID
     * @return 报名记录
     */
    @PostMapping("/{activityId}/register")
    public ResponseEntity<ActivityRegistration> registerForActivity(@PathVariable Long activityId, 
                                                                  @RequestParam Long studentId) {
        ActivityRegistration registration = activityService.registerForActivity(activityId, studentId);
        if (registration != null) {
            return ResponseEntity.ok(registration);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 取消活动报名
     * @param activityId 活动ID
     * @param studentId 学生ID
     * @return 操作结果
     */
    @DeleteMapping("/{activityId}/register")
    public ResponseEntity<String> cancelRegistration(@PathVariable Long activityId, 
                                                    @RequestParam Long studentId) {
        boolean result = activityService.cancelRegistration(activityId, studentId);
        if (result) {
            return ResponseEntity.ok("取消报名成功");
        } else {
            return ResponseEntity.badRequest().body("取消报名失败");
        }
    }

    /**
     * 根据活动ID获取报名记录
     * @param activityId 活动ID
     * @return 报名记录列表
     */
    @GetMapping("/{activityId}/registrations")
    public ResponseEntity<List<ActivityRegistration>> getRegistrationsByActivityId(@PathVariable Long activityId) {
        List<ActivityRegistration> registrations = activityService.getRegistrationsByActivityId(activityId);
        return ResponseEntity.ok(registrations);
    }

    /**
     * 根据学生ID获取报名记录
     * @param studentId 学生ID
     * @return 报名记录列表
     */
    @GetMapping("/registrations/student/{studentId}")
    public ResponseEntity<List<ActivityRegistration>> getRegistrationsByStudentId(@PathVariable Long studentId) {
        List<ActivityRegistration> registrations = activityService.getRegistrationsByStudentId(studentId);
        return ResponseEntity.ok(registrations);
    }
}